package com.ruoyi.web.controller.price;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.IGrDetailService;
import com.ruoyi.web.domain.GrDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/price/grDetail")
public class GrDetailController extends BaseController {
    private String prefix = "price/grDetail";

    @Autowired
    private IGrDetailService grDetailService;

    @GetMapping("/list/{categoryId}")
    public String list(@PathVariable("categoryId") Integer categoryId, ModelMap mmap) {
        startPage();
        List<GrDetail> list = grDetailService.selectByCategoryId(categoryId);
        mmap.put("grDetails", list);
        mmap.put("categoryId", categoryId);
        return prefix + "/list";
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{grDetailId}")
    public String edit(@PathVariable("grDetailId") Integer grDetailId, ModelMap mmap){
        mmap.put("grDetail", grDetailService.selectGrDetailById(grDetailId));
        return prefix + "/edit";
    }

    @GetMapping("/delete/{grDetailId}")
    public String delete(@PathVariable("grDetailId") Integer grDetailId, ModelMap mmap){
        GrDetail role = grDetailService.selectGrDetailById(grDetailId);
        if(Objects.nonNull(role)){
            grDetailService.deleteGrDetailById(grDetailId);
        }
        return redirect("/price/grDetail/list/" + role.getVcrId());
    }

    /**
     * 修改保存角色
     */
    @PostMapping("/edit")
    public String editSave(@Validated GrDetail role) {
        int vrt = grDetailService.updateGrDetail(role);
        return redirect("/price/grDetail/list/" + vrt);
    }

    @GetMapping("/toSave/{categoryId}")
    public String toSave(@PathVariable("categoryId") Integer categoryId, ModelMap mmap){
        GrDetail detail = new GrDetail();
        detail.setVcrId(categoryId);
        mmap.put("grDetail", detail);
        return prefix + "/add";
    }

    @PostMapping("/save")
//    @ResponseBody
    public String saveGrDetail(@Validated GrDetail role) {
         grDetailService.saveGrDetail(role);
        return redirect("/price/grDetail/list/" + role.getVcrId());
    }
}
